package com.lazyshopkeeper.front.service.impl;

import java.util.ArrayList;
import java.util.List;


import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.lazyshopkeeper.front.common.Constant;
import com.lazyshopkeeper.front.domain.DutyLemplate;
import com.lazyshopkeeper.front.service.IDutyService;
import com.lazyshopkeeper.front.service.IInnerStoreService;
import com.taobao.api.ApiException;
import com.taobao.api.domain.Item;

@Service
public class InnerStoreService implements IInnerStoreService{

	private static Logger logger = LoggerFactory.getLogger(InnerStoreService.class);
	@Autowired
	private IDutyService dutyService;
	@Override
	public List<Item>  getDesc(String styleType, String tuiguangName,String showType, String sortType,
		int cosCount, int rowCount, List<Item> items,String sessionKey) throws ApiException  {
		
		List<Item> litems=new ArrayList<Item>();
		String desc=this.getInnerLamplate();
		//店内推广中间部分
		String innerbody="";
		//商品标题
		String itemTitlte="";
		//修改模板属性
		desc=desc.replaceAll("${styleType}", styleType);
		desc=desc.replaceAll("${tuiguangName}", tuiguangName);
		desc=desc.replaceAll("${cosCount}", cosCount+"");
		desc=desc.replaceAll("${rowCount}", rowCount+"");
		innerbody=desc.substring(desc.indexOf("${loopStart}"),desc.indexOf("${loopEnd}"));
		if(innerbody!=""){
			for(int i =0;i<cosCount;i++){
				innerbody="<tr>"+innerbody;
				if(items!=null){
					for(int j=0+rowCount*i ;j<items.size();j++){
						if(j%rowCount==0&&j!=0){
							break;
						}
						else{
							innerbody.replaceAll("${Strtitle}", items.get(j).getTitle());
							innerbody.replaceAll("{$price}",  items.get(j).getPrice());
							innerbody.replaceAll("${pirUrl}", items.get(j).getPicUrl());
							innerbody.replaceAll("${itemUrl}", items.get(j).getDetailUrl());
						}
					}
				}
				innerbody=innerbody+"</tr>";
			}
			
		}
		
		desc=desc.substring(0, desc.indexOf("${loopStart}"))+innerbody+desc.substring(desc.indexOf("${loopEnd}"),desc.length() );
		
		for(Item item:items){
			if(showType!=null&&showType.equals("1"))
				item.setDesc(desc+item.getDesc());
			else if(showType!=null&&showType.equals("2"))
				item.setDesc(item.getDesc()+desc);
			litems.add(item);
		}
		return litems;
	}

	
	public String getInnerLamplate(){
		DutyLemplate dutyLemplate=dutyService.getDutyLemplateBytype(Constant.INNERSTORE_TEMP);
		return dutyLemplate.getTemplate();
	}
	
//	public String getItemTitle(String title){
//		if(title==null)
//			return null;
//		String Strtitle="";
//		int tempLength=title.length();
//		int byteLength = title.getBytes().length;
//		for(int i=0;i<(byteLength+tempLength)/2/15;i++){
//			Strtitle+=title.substring(i*15,i*15+15)+"</br>";	
//	    }
//		return Strtitle;
//	}
	
	public static void main(String[] args) {
		String str="afa发嗲";
		int i = str.length();
		int j = str.getBytes().length;
		
		System.out.println(j/2+i/2);
	}
}
